package cii_lib_graph;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import testy.TestHardware;
import cii_functions.FCII_IDENTIFICATOR;
import cii_lib.CCII_ACCESS_INTERF;
import cii_lib.CCII_ACCESS_PARAM;
import cii_lib.CIIdata;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GCII_IDENTYFICATOR extends javax.swing.JPanel implements plugins.ICIIGraphicable {
	
	private static final Logger log = LoggerFactory
			.getLogger(GCII_IDENTYFICATOR.class);

	 private Vector<CCII_ACCESS_INTERF> interfaces = new Vector<CCII_ACCESS_INTERF>();
	 private Vector<CCII_ACCESS_PARAM> params = new Vector<CCII_ACCESS_PARAM>();
	 private String cpath = null;
	 private FCII_IDENTIFICATOR tCii;
	 
	public void test() {
		JFrame frame = new JFrame();
		frame.getContentPane().add(this);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.setSize(371, 364);
		frame.setPreferredSize(new Dimension(380, 395));
		frame.pack();
		frame.setVisible(true);
	}
	
	public GCII_IDENTYFICATOR() {
		super();
		initGUI();
	}
	public GCII_IDENTYFICATOR(final FCII_IDENTIFICATOR cid) {
		super();
		cpath = cid.getComponent().PathName();
//		cpath = "$CII_MUX_DATA_TRANSCEIVER.COMP_ID[0]";
		interfaces = cid.getComponent().getInterfaces();
		params = cid.getComponent().getParams();
		tCii = cid;
		if (cid instanceof FCII_IDENTIFICATOR) {
			log.debug("FUNCTIONAL!");
		}
		initGUI();
	}
	
	private static final long serialVersionUID = 2L;
	private JPanel ciiFuncPanel;
	private JButton jButton2;
	private JTextField jTextField6;
	private JLabel jLabel4;
	private JButton jButtonSelect;
	private JTextPane logPane1;
	private JScrollPane jScrollPane1;
	private JButton jButton8;
	private JTextField checkTextField11;
	private JButton jButton7;
	private JPanel jaccPanel1;
	private JLabel jLabel5;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JLabel jico1;
	private JTextField jTextField10;
	private JTextField jTextField9;
	private JTextField jTextField8;
	private JTextField jTextField7;
	private JTextField jTextField5;
	private JTextField jTextField4;
	private JTextField jTextField3;
	private JTextField jTextField2;
	private JTextField jTextField1;
	private JLabel jLabel1;
	private JLabel jLabelS;
	private JButton jButton6;
	private JButton jButton5;
	private JButton jButton4;
	private JButton jButton3;
	private JButton jButton1;
	private JPanel testBsPanel;
	private JPanel ciiInPanel;
	
	private Icon gi = new ImageIcon("resource\\Aqua-Ball-Green-16x16.png");
	private Icon ri = new ImageIcon("resource\\Aqua-Ball-Red-16x16.png");

	private int randCount=0;
	
	private FCII_IDENTIFICATOR functions;
	
	private void initGUI() {
		TestHardware.incrementType = 3;
		try {
			setLayout(null);
			this.setPreferredSize(new java.awt.Dimension(371, 364));
			{
				ciiFuncPanel = new JPanel();
				AnchorLayout ciiFuncPanelLayout = new AnchorLayout();
				add(ciiFuncPanel);
				ciiFuncPanel.setBounds(0, 0, 371, 364);
				ciiFuncPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0,0,0)), "CCII_IDENTIFICATOR", TitledBorder.LEADING, TitledBorder.TOP));
				ciiFuncPanel.setLayout(ciiFuncPanelLayout);
				{
					ciiInPanel = new JPanel();
					ciiFuncPanel.add(ciiInPanel, new AnchorConstraint(41, 990, 991, 9, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					ciiInPanel.setBorder(BorderFactory.createTitledBorder(null, cpath, TitledBorder.LEADING, TitledBorder.TOP, new java.awt.Font("Dialog",1,12), new java.awt.Color(128,0,64)));
					ciiInPanel.setPreferredSize(new java.awt.Dimension(364, 399));
					ciiInPanel.setLayout(null);
					{
						testBsPanel = new JPanel();
						ciiInPanel.add(testBsPanel);
						testBsPanel.setBounds(14, 28, 336, 196);
						testBsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(128,128,255)), "Check with hardware: ", TitledBorder.LEADING, TitledBorder.TOP, new java.awt.Font("Dialog",1,12), new java.awt.Color(46,101,184)));
						testBsPanel.setLayout(null);
						{
							jButton1 = new JButton();
							testBsPanel.add(jButton1);
							jButton1.setText("IDENTYFIER");
							jButton1.setBounds(25, 56, 105, 21);
							jButton1.setEnabled(tCii.getComponent().getParamByName("LPAR_IDENTYFIER_CII").getBool());
							jButton1.setFont(new java.awt.Font("Tahoma",0,10));
							jButton1.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand()); 
									testID();
								}
							});
						}
						{
							jButton2 = new JButton();
							testBsPanel.add(jButton2);
							jButton2.setText("NAME");
							jButton2.setEnabled(tCii.getComponent().getParamByName("LPAR_NAME_CII").getBool()); //(1>2)?true:false);
							jButton2.setFont(new java.awt.Font("Tahoma",0,10));
							jButton2.setBounds(25, 84, 105, 21);
							jButton2.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
									testName();									
								}
							});
						}
						{
							jButton3 = new JButton();
							testBsPanel.add(jButton3);
							jButton3.setText("VERSION");
							jButton3.setEnabled(tCii.getComponent().getParamByName("LPAR_VERSION_CII").getBool());
							jButton3.setFont(new java.awt.Font("Tahoma",0,10));
							jButton3.setBounds(25, 112, 105, 21);
							jButton3.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
									testVersion();
								}
							});
						}
						{
							jButton4 = new JButton();
							testBsPanel.add(jButton4);
							jButton4.setText("USER");
							jButton4.setFont(new java.awt.Font("Tahoma",0,10));
							jButton4.setBounds(25, 140, 105, 21);
							jButton4.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
									testUser();
								}
							});
						}
						{
							jButton5 = new JButton();
							testBsPanel.add(jButton5);
							jButton5.setText("CREATOR");
							jButton5.setEnabled(tCii.getComponent().getParamByName("LPAR_CREATOR_CII").getBool());
							jButton5.setFont(new java.awt.Font("Tahoma",0,10));
							jButton5.setBounds(25, 168, 105, 21);
							jButton5.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
									testCreator();
								}
							});
						}
						{
							jButton6 = new JButton();
							testBsPanel.add(jButton6);
							jButton6.setText("ALL");
							jButton6.setFont(new java.awt.Font("Tahoma",0,10));
							jButton6.setBounds(39, 28, 70, 21);
							jButton6.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { 
									log.info("Command: {}", evt.getActionCommand());
									log.debug("test all: "+testAll());
								}
							});
						}
						{
							jLabelS = new JLabel();
							testBsPanel.add(jLabelS);
							jLabelS.setText("Soft");
							jLabelS.setBounds(167, 28, 42, 21);
						}
						{
							jLabel1 = new JLabel();
							testBsPanel.add(jLabel1);
							jLabel1.setText("Hard");
							jLabel1.setBounds(263, 28, 42, 21);
						}
						{
							jTextField1 = new JTextField();
							testBsPanel.add(jTextField1);
							jTextField1.setText("0");
							jTextField1.setBounds(130, 56, 98, 21);
							jTextField1.setEditable(false);
							jTextField1.setColumns(2);
							jTextField1.setHorizontalAlignment(SwingConstants.CENTER);
						}
						{
							jTextField2 = new JTextField();
							testBsPanel.add(jTextField2);
							jTextField2.setColumns(2);
							jTextField2.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField2.setText("0");
							jTextField2.setEditable(false);
							jTextField2.setBounds(228, 56, 98, 21);
						}
						{
							jTextField3 = new JTextField();
							testBsPanel.add(jTextField3);
							jTextField3.setColumns(2);
							jTextField3.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField3.setText("0");
							jTextField3.setEditable(false);
							jTextField3.setBounds(130, 84, 98, 21);
						}
						{
							jTextField4 = new JTextField();
							testBsPanel.add(jTextField4);
							jTextField4.setColumns(2);
							jTextField4.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField4.setText("0");
							jTextField4.setEditable(false);
							jTextField4.setBounds(228, 84, 98, 21);
						}
						{
							jTextField5 = new JTextField();
							testBsPanel.add(jTextField5);
							jTextField5.setColumns(2);
							jTextField5.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField5.setText("0");
							jTextField5.setEditable(false);
							jTextField5.setBounds(130, 112, 98, 21);
						}
						{
							jTextField6 = new JTextField();
							testBsPanel.add(jTextField6);
							jTextField6.setColumns(2);
							jTextField6.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField6.setText("0");
							jTextField6.setEditable(false);
							jTextField6.setBounds(228, 112, 98, 21);
						}
						{
							jTextField7 = new JTextField();
							testBsPanel.add(jTextField7);
							jTextField7.setColumns(2);
							jTextField7.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField7.setText("0");
							jTextField7.setEditable(false);
							jTextField7.setBounds(130, 140, 98, 21);
						}
						{
							jTextField8 = new JTextField();
							testBsPanel.add(jTextField8);
							jTextField8.setColumns(2);
							jTextField8.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField8.setText("0");
							jTextField8.setEditable(false);
							jTextField8.setBounds(228, 140, 98, 21);
						}
						{
							jTextField9 = new JTextField();
							testBsPanel.add(jTextField9);
							jTextField9.setColumns(2);
							jTextField9.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField9.setText("0");
							jTextField9.setEditable(false);
							jTextField9.setBounds(130, 168, 98, 21);
						}
						{
							jTextField10 = new JTextField();
							testBsPanel.add(jTextField10);
							jTextField10.setColumns(2);
							jTextField10.setHorizontalAlignment(SwingConstants.CENTER);
							jTextField10.setText("0");
							jTextField10.setEditable(false);
							jTextField10.setBounds(228, 168, 98, 21);
						}
						{
							jico1 = new JLabel();
							testBsPanel.add(jico1);
							jico1.setIcon(gi);
							jico1.setBounds(6, 56, 21, 21);
							jico1.setEnabled(false);
						}
						{
							jLabel2 = new JLabel();
							testBsPanel.add(jLabel2);
							jLabel2.setIcon(gi);
							jLabel2.setEnabled(false);
							jLabel2.setBounds(6, 84, 21, 21);
						}
						{
							jLabel3 = new JLabel();
							testBsPanel.add(jLabel3);
							jLabel3.setIcon(gi);
							jLabel3.setEnabled(false);
							jLabel3.setBounds(6, 112, 21, 21);
						}
						{
							jLabel4 = new JLabel();
							testBsPanel.add(jLabel4);
							jLabel4.setIcon(gi);
							jLabel4.setEnabled(false);
							jLabel4.setBounds(6, 140, 21, 21);
						}
						{
							jLabel5 = new JLabel();
							testBsPanel.add(jLabel5);
							jLabel5.setIcon(gi);
							jLabel5.setEnabled(false);
							jLabel5.setBounds(6, 168, 21, 21);
						}
					}
					{
						jaccPanel1 = new JPanel();
						ciiInPanel.add(jaccPanel1);
						jaccPanel1.setBounds(14, 231, 336, 105);
						jaccPanel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(159,32,0)), "Check access: ", TitledBorder.LEADING, TitledBorder.TOP));
						jaccPanel1.setLayout(null);
						{
							jButton7 = new JButton();
							jaccPanel1.add(jButton7);
							jButton7.setText("Check with:");
							jButton7.setFont(new java.awt.Font("Tahoma",0,10));
							jButton7.setBounds(21, 21, 91, 21);
							jButton7.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { 
									log.info("Command: {} {}",  new Object[] {evt.getActionCommand(), checkTextField11.getText()});
									TestHardware.incrementType=3;
								}
							});
						}
						{
							checkTextField11 = new JTextField();
							jaccPanel1.add(checkTextField11);
							checkTextField11.setBounds(118, 21, 112, 21);
						}
						{
							jButton8 = new JButton();
							jaccPanel1.add(jButton8);
							jButton8.setText("Random");
							jButton8.setFont(new java.awt.Font("Tahoma",0,10));
							jButton8.setBounds(238, 21, 91, 21);
							final Dimension xy= new Dimension();
							jButton8.addMouseListener(new MouseAdapter() {
								public void mousePressed(MouseEvent evt) {
									xy.width = evt.getXOnScreen();
									xy.height= evt.getYOnScreen();
								}
							});
							jButton8.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { 
									log.info("Command: {}", evt.getActionCommand());
									JDialog howmanyD = new JDialog(new Dialog(
										new Frame()), true);
									JTextField samplesCount = null;
									howmanyD.setTitle("Samples");
									howmanyD.setLayout(null);
									{
										samplesCount = new JTextField();
										howmanyD.add(samplesCount);
										samplesCount.setText("how many?");
										final JTextField kpole = samplesCount;
										final JDialog tmp = howmanyD;
										samplesCount.addKeyListener(new KeyListener() {

												public void keyTyped(KeyEvent e) {
													int len = kpole.getText()
														.length();
													//											    if (kpole.getSelectionEnd()==9) return;
													if (len < 9) {
														return;
													} else {
														e.consume();
														kpole
															.setCaretPosition(9);
													}
												}
												public void keyReleased(
													KeyEvent e) {
												}
												public void keyPressed(
													KeyEvent e) {
													char c = e.getKeyChar();
													if (c == KeyEvent.VK_ENTER) {
														try {
															randCount = new Integer(
																kpole.getText());
															logln("Samples count: "
																+ randCount);
															tmp.dispose();
															StringBuffer sbuf = new StringBuffer();
															tCii
																.writeRndSamples(
																	randCount,
																	sbuf);
															logln(sbuf
																.deleteCharAt(
																	sbuf
																		.length() - 1)
																.toString());
															sbuf = null;
														} catch (NumberFormatException err) {
															kpole
																.setText("wrong !");
															kpole.selectAll();
														}
														return;
													} else if (c == KeyEvent.VK_ESCAPE) {
														tmp.dispose();
														return;
													}
												}
											});
										samplesCount.selectAll();
										samplesCount.setBounds(7, 14, 70, 21);
									}
									{
										JButton jButtonD1 = new JButton();
										howmanyD.add(jButtonD1);
										jButtonD1.setText("Ok");
										final JDialog tmp = howmanyD;
										final JTextField tmpj = samplesCount;
										jButtonD1
											.addActionListener(new ActionListener() {
												public void actionPerformed(
													ActionEvent evt) {
													log.info("Command: {} samples count {}", new Object[] {evt.getActionCommand(), tmpj.getText()});
													try {
														randCount = new Integer(
															tmpj.getText());
														logln("Samples count: "
															+ randCount);
														tmp.dispose();
														StringBuffer sbuf = new StringBuffer();
														tCii.writeRndSamples(randCount,sbuf);
														logln(sbuf
															.deleteCharAt(
																sbuf.length() - 1)
															.toString());
														sbuf = null;
													} catch (NumberFormatException err) {
														tmpj.setText("wrong !");
														tmpj.selectAll();
													}
												}

											});
										jButtonD1.setBounds(77, 14, 63, 21);
									}
									{
										JButton jButtonD2 = new JButton();
										howmanyD.add(jButtonD2);
										jButtonD2.setText("Cancel");
										final JDialog tmp = howmanyD;
										jButtonD2
											.addActionListener(new ActionListener() {
												public void actionPerformed(
													ActionEvent evt) {
													log.info("Command: {}", evt.getActionCommand());
													tmp.dispose();
												}
											});
										jButtonD2.setBounds(35, 42, 77, 21);
									}
									howmanyD.setSize(155, 99);
//								    howmanyD.setLocation(getMousePosition().x-100, getMousePosition().y-40);
									howmanyD.setLocation(xy.width, xy.height);
									howmanyD.setVisible(true);
								}
							});
						}
						{
							jScrollPane1 = new JScrollPane();
							jaccPanel1.add(jScrollPane1);
							jScrollPane1.setBounds(21, 49, 308, 49);
							{
								logPane1 = new JTextPane();
								jScrollPane1.setViewportView(logPane1);
								logPane1.setEditable(false);
								logPane1.setFont(new java.awt.Font("Tahoma",0,10));
							}
						}
						{
							jButtonSelect = new JButton();
							jaccPanel1.add(jButtonSelect);
							jButtonSelect.setText("s");
							jButtonSelect.setBounds(7, 83, 14, 14);
							jButtonSelect.setFont(new java.awt.Font("Tahoma",0,10));
							jButtonSelect.setToolTipText("Select all");
							jButtonSelect
								.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
									logPane1.grabFocus();
									logPane1.selectAll();									
								}
								});
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("", e);
		}
	}
	
	private void logln(String arg) {
		logPane1.selectAll();
		logPane1.setCaretPosition(logPane1.getSelectionEnd());
		logPane1.setEditable(true);
		logPane1.replaceSelection(arg+"\n");
		logPane1.setEditable(false);
	}
	
	private void testName() {
		CIIdata datS = new CIIdata(1), datH = new CIIdata(1);
		boolean tmp = ((FCII_IDENTIFICATOR)tCii).testName(datS,datH);
		jTextField3.setText(datS.GetStr());
		jTextField4.setText(datH.GetStr());
		if (tmp) {
			logln("Name - OK");
			jLabel2.setIcon(gi);
			jLabel2.setEnabled(true);
		}
		else {
			logln("Name - WRONG");
			jLabel2.setIcon(ri);
			jLabel2.setEnabled(true);
		}
	
	}
	
	private void testID() {
		CIIdata datS = new CIIdata(1), datH = new CIIdata(1);
		boolean tmp = ((FCII_IDENTIFICATOR)tCii).testID(datS,datH);
		jTextField1.setText(datS.GetStr());
		jTextField2.setText(datH.GetStr());
		if (tmp) {
			logln("Identyfier - OK");
			jico1.setIcon(gi);
			jico1.setEnabled(true);
		}
		else {
			logln("Identyfier - WRONG");
			jico1.setIcon(ri);
			jico1.setEnabled(true);
		}
	}
	
	private void testVersion() {
		CIIdata datS = new CIIdata(1), datH = new CIIdata(1);
		boolean tmp = ((FCII_IDENTIFICATOR)tCii).testVersion(datS,datH);
		jTextField5.setText(datS.GetStr());
		jTextField6.setText(datH.GetHex());
		if (tmp) {
			logln("Version - OK");
			jLabel3.setIcon(gi);
			jLabel3.setEnabled(true);
		}
		else {
			logln("Version - WRONG");
			jLabel3.setIcon(ri);
			jLabel3.setEnabled(true);
		}
	
	}
	
	private void testCreator() {
		CIIdata datS = new CIIdata(1), datH = new CIIdata(1);
		
		boolean tmp = ((FCII_IDENTIFICATOR)tCii).testCreator(datS,datH);
		jTextField9.setText(datS.GetStr());
		jTextField10.setText(datH.GetStr());
		if (tmp) {
			logln("Creator - OK");
			jLabel5.setIcon(gi);
			jLabel5.setEnabled(true);
		}
		else {
			logln("Creator - WRONG");
			jLabel5.setIcon(ri);
			jLabel5.setEnabled(true);
		}
	}
	
	private void testUser() {
		CIIdata datS = new CIIdata(1), datH = new CIIdata(1);
		boolean tmp = ((FCII_IDENTIFICATOR)tCii).testUser(datS,datH);
		jTextField7.setText(datS.GetStr());
		jTextField8.setText(datH.GetStr());
		if (tmp) {
			logln("User - OK");
			jLabel4.setIcon(gi);
			jLabel4.setEnabled(true);
		}
		else {
			logln("User - WRONG");
			jLabel4.setIcon(ri);
			jLabel4.setEnabled(true);
		}
	}
	
	public boolean testAll() {
		testName();
		testID();
		testVersion();
		testCreator();
		testUser();
		return tCii.testAll();
	}

	/* (non-Javadoc)
	 * @see plugins.ICIIGraphicable#getCustomPane(java.lang.String)
	 */
	public JPanel getCustomPane(String xmlconfig) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see plugins.ICIIGraphicable#getDefaultPane()
	 */
	public JPanel getDefaultPane() {
		// TODO Auto-generated method stub
		return ciiFuncPanel;
	}

	/* (non-Javadoc)
	 * @see plugins.ICIIGraphicable#getGCIIName()
	 */
	public String getGCIIName() {
		// TODO Auto-generated method stub
		return "GCII_IDENTYFICATOR";
	}
	
}
